﻿'<REA Tool is a open source Tool for Get Linux a lightweight download client for Operating Systems.>
'    Copyright (C) <2011>  <Dev-fire>

'    This program is free software: you can redistribute it and/or modify
'    it under the terms of the GNU General Public License as published by
'    the Free Software Foundation, either version 3 of the License, or
'    (at your option) any later version.

'    This program is distributed in the hope that it will be useful,
'    but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>.
Public Class Data
    'Build Operating Systems List For ListBox Operating Systems
    Shared Sub Build_Operating_Systems_List()
        Main.ListBox_Operating_Systems_1.Items.Clear()
        Main.ListBox_Operating_Systems_2.Items.Clear()
        Dim OSD = Directory.GetDirectories(Application.StartupPath & "\Data\Operating Systems\")
        'Write OS.cfg
        For Each OS In OSD
            Main.ListBox_Operating_Systems_1.Items.Add(Path.GetFileName(OS))
            Main.ListBox_Operating_Systems_2.Items.Add(Path.GetFileName(OS))
            Main.AutoComplete_Operating_Systems.Add(Path.GetFileName(OS))
        Next
        Main.TextBox_Search_For_OS.AutoCompleteCustomSource = Main.AutoComplete_Operating_Systems
    End Sub
    'Add OS
    Shared Sub Add_OS()
        Try
            'Create Directory For OS If The Directory Does Not Exists
            If Not Directory.Exists(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text) Then
                Directory.CreateDirectory(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text)
            End If
            'Write OS.cfg
            Dim Write_OS_CFG As New StreamWriter(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text & "\OS.cfg")
            Write_OS_CFG.WriteLine("About:" & Main.TextBox_About_1.Text.Replace(vbCrLf, Nothing))
            Write_OS_CFG.WriteLine("Origin:" & Main.TextBox_Origin_1.Text)
            Write_OS_CFG.WriteLine("Version:" & Main.TextBox_Version_1.Text)
            Write_OS_CFG.WriteLine("Desktop:" & Main.TextBox_Desktop_1.Text)
            Write_OS_CFG.WriteLine("Website:" & Main.TextBox_Website_1.Text)
            Write_OS_CFG.WriteLine("Download_32:" & Main.TextBox_Download_32_Bit_1.Text)
            Write_OS_CFG.WriteLine("FileSize_32:" & Main.TextBox_File_Size_32_Bit_1.Text)
            Write_OS_CFG.WriteLine("Download_64:" & Main.TextBox_Download_64_Bit_1.Text)
            Write_OS_CFG.WriteLine("FileSize_64:" & Main.TextBox_File_Size_64_Bit_1.Text)
            Write_OS_CFG.Close()
            Write_OS_CFG.Dispose()
            'Save OS Icon
            If File.Exists(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text & "\Icon.png") Then
                File.Delete(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text & "\Icon.png")
                Main.OS_Icon_Image.Save(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text & "\Icon.png", ImageFormat.Png)
            Else
                Main.OS_Icon_Image.Save(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text & "\Icon.png", ImageFormat.Png)
            End If
            'Save OS Image
            If File.Exists(Application.StartupPath & "\Data\Operating Systems\" & "\" & Main.TextBox_OS_Title_1.Text & "\Image.jpg") Then
                File.Delete(Application.StartupPath & "\Data\Operating Systems\" & "\" & Main.TextBox_OS_Title_1.Text & "\Image.jpg")
                Main.OS_Image.Save(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text & "\Image.jpg", ImageFormat.Jpeg)
            Else
                Main.OS_Image.Save(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_1.Text & "\Image.jpg", ImageFormat.Jpeg)
            End If
            'Write OSL.cfg
            Write_OSL_cfg()
            'Rebuild Operating Systems List
            Data.Build_Operating_Systems_List()
            'Count Operating Systems
            Data.OS_Count()
            'OS Added MSG
            MsgBox("OS: " & Main.TextBox_OS_Title_1.Text & " Added.", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    'Edit OS
    Shared Sub Edit_OS()
        Try
            'If Nothing Has Been Changed
            If Main.Selected_OS = Main.TextBox_OS_Title_2.Text And Main.About = Main.TextBox_About_2.Text And Main.Website = Main.TextBox_Website_2.Text And Main.Desktop = Main.TextBox_Desktop_2.Text And Main.Origin = Main.TextBox_Origin_2.Text And Main.Version = Main.TextBox_Version_2.Text _
            And Main.Download_32 = Main.TextBox_Download_32_Bit_2.Text And Main.Download_64 = Main.TextBox_Download_64_Bit_2.Text And Main.File_Size_32 = Main.TextBox_File_Size_32_Bit_2.Text _
            And Main.File_Size_64 = Main.TextBox_File_Size_64_Bit_2.Text Then
            Else
                If Main.ListBox_Operating_Systems_2.SelectedItem.ToString = Main.TextBox_OS_Title_2.Text = False Then
                    'Rename OS
                    Rename(Application.StartupPath & "\Data\Operating Systems\" & Main.Selected_OS, Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_2.Text)
                    'write OSL.cfg
                    Write_OSL_cfg()
                    'Rebuild Operating Systems List
                    Data.Build_Operating_Systems_List()
                End If
                'Write OS.cfg
                Dim Write_OS_CFG As New StreamWriter(Application.StartupPath & "\Data\Operating Systems\" & Main.TextBox_OS_Title_2.Text & "\OS.cfg")
                Write_OS_CFG.WriteLine("About:" & Main.TextBox_About_2.Text.Replace(vbCrLf, Nothing))
                Write_OS_CFG.WriteLine("Origin:" & Main.TextBox_Origin_2.Text)
                Write_OS_CFG.WriteLine("Version:" & Main.TextBox_Version_2.Text)
                Write_OS_CFG.WriteLine("Desktop:" & Main.TextBox_Desktop_2.Text)
                Write_OS_CFG.WriteLine("Website:" & Main.TextBox_Website_2.Text)
                Write_OS_CFG.WriteLine("Download_32:" & Main.TextBox_Download_32_Bit_2.Text)
                Write_OS_CFG.WriteLine("FileSize_32:" & Main.TextBox_File_Size_32_Bit_2.Text)
                Write_OS_CFG.WriteLine("Download_64:" & Main.TextBox_Download_64_Bit_2.Text)
                Write_OS_CFG.WriteLine("FileSize_64:" & Main.TextBox_File_Size_64_Bit_2.Text)
                Write_OS_CFG.Close()
                Write_OS_CFG.Dispose()
            End If
                'Save OS Icon
                If Not Main.PictureBox_OS_Icon_2.ImageLocation = Main.OS_Icon_Image_String Then
                    If File.Exists(Application.StartupPath & "\Data\Operating Systems\" & Main.ListBox_Operating_Systems_2.SelectedItem.ToString & "\Icon.png") Then
                        File.Delete(Application.StartupPath & "\Data\Operating Systems\" & Main.ListBox_Operating_Systems_2.SelectedItem.ToString & "\Icon.png")
                        Main.OS_Icon_Image.Save(Application.StartupPath & "\Data\Operating Systems\" & Main.ListBox_Operating_Systems_2.SelectedItem.ToString & "\Icon.png", ImageFormat.Png)
                    Else
                        Main.OS_Icon_Image.Save(Application.StartupPath & "\Data\Operating Systems\" & Main.ListBox_Operating_Systems_2.SelectedItem.ToString & "\Icon.png", ImageFormat.Png)
                    End If
                End If
                'Save OS Image
                If Not Main.PictureBox_OS_Image_2.ImageLocation = Main.OS_Image_String Then
                    If File.Exists(Application.StartupPath & "\Data\Operating Systems\" & "\" & Main.ListBox_Operating_Systems_2.SelectedItem.ToString & "\Image.jpg") Then
                        File.Delete(Application.StartupPath & "\Data\Operating Systems\" & "\" & Main.ListBox_Operating_Systems_2.SelectedItem.ToString & "\Image.jpg")
                        Main.OS_Image.Save(Application.StartupPath & "\Data\Operating Systems\" & Main.ListBox_Operating_Systems_2.SelectedItem.ToString & "\Image.jpg", ImageFormat.Jpeg)
                    Else
                        Main.OS_Image.Save(Application.StartupPath & "\Data\Operating Systems\" & Main.ListBox_Operating_Systems_2.SelectedItem.ToString & "\Image.jpg", ImageFormat.Jpeg)
                    End If
                End If
                'OS Edited MSG
            MsgBox("Operating System: " & Main.TextBox_OS_Title_2.Text & " Edited.", MsgBoxStyle.Information)
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    'Remove OS
    Shared Sub Remove_OS()
        Try
            Dim OSTR As String = Main.ListBox_Operating_Systems_1.SelectedItem.ToString
            'Delete OS
            If Directory.Exists(Application.StartupPath & "\Data\Operating Systems\" & Main.ListBox_Operating_Systems_1.SelectedItem.ToString) Then
                Directory.Delete(Application.StartupPath & "\Data\Operating Systems\" & Main.ListBox_Operating_Systems_1.SelectedItem.ToString, True)
            End If
            'Delete OSL.cfg In The Selected Category If The File Exists
            If File.Exists(Application.StartupPath & "\Data\Operating Systems\OSL.cfg") Then
                File.Delete(Application.StartupPath & "\Data\Operating Systems\OSL.cfg")
            End If
            '
            If Not Main.ListBox_Operating_Systems_2.SelectedIndex = -1 Then
                If Main.ListBox_Operating_Systems_1.SelectedItem.ToString = Main.ListBox_Operating_Systems_2.SelectedItem.ToString Then
                    Main.ListBox_Operating_Systems_2.SelectedIndex = -1
                End If
            End If
            'Write OSL.cfg
            Write_OSL_cfg()
            'Rebuild Operating Systems List
            Build_Operating_Systems_List()
            'Count Operating Systems
            Data.OS_Count()
            'OS Removed Message
            MsgBox(OSTR & " removed.", MsgBoxStyle.Information)
            'Clear OSTR
            OSTR = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    'Write OSL.cfg
    Shared Sub Write_OSL_cfg()
        Dim Write_OSL_CFG As New StreamWriter(Application.StartupPath & "\Data\Operating Systems\OSL.cfg")
        Dim OSD = Directory.GetDirectories(Application.StartupPath & "\Data\Operating Systems\")
        'Write OSL.cfg
        For Each OS In OSD
            Write_OSL_CFG.WriteLine(Path.GetFileName(OS))
        Next
        Write_OSL_CFG.Close()
    End Sub
    'Count Operating Systems
    Shared Sub OS_Count()
        Dim Operating_Systems_Count As Integer
        Operating_Systems_Count = File.ReadAllLines(Application.StartupPath & "\Data\Operating Systems\OSL.cfg").Count
        Main.Text = "REA Tool - " & Operating_Systems_Count.ToString & " Operating Systems"
    End Sub
    'Clear GUI And Data
    Shared Sub Clear_GUI_And_Data()
        'DATA
        Main.Selected_OS = Nothing
        Main.About = Nothing
        Main.Version = Nothing
        Main.Website = Nothing
        Main.Desktop = Nothing
        Main.Origin = Nothing
        Main.Download_32 = Nothing
        Main.File_Size_32 = Nothing
        Main.Download_64 = Nothing
        Main.File_Size_64 = Nothing
        'UI
        Main.PictureBox_OS_Icon_2.ImageLocation = Nothing
        Main.PictureBox_OS_Image_2.ImageLocation = Nothing
        Main.TextBox_Download_32_Bit_2.Clear()
        Main.TextBox_File_Size_32_Bit_2.Clear()
        Main.TextBox_Download_64_Bit_2.Clear()
        Main.TextBox_File_Size_64_Bit_2.Clear()
        Main.TextBox_OS_Title_2.Clear()
        Main.TextBox_About_2.Clear()
        Main.TextBox_Origin_2.Clear()
        Main.TextBox_Version_2.Clear()
        Main.TextBox_Website_2.Clear()
        Main.TextBox_Desktop_2.Clear()
        'Reset Edit Colors
        If Not Main.TextBox_Download_32_Bit_2.ForeColor = Color.Black Then
            Main.TextBox_Download_32_Bit_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_File_Size_32_Bit_2.ForeColor = Color.Black Then
            Main.TextBox_File_Size_32_Bit_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_Download_64_Bit_2.ForeColor = Color.Black Then
            Main.TextBox_Download_64_Bit_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_File_Size_64_Bit_2.ForeColor = Color.Black Then
            Main.TextBox_File_Size_64_Bit_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_OS_Title_2.ForeColor = Color.Black Then
            Main.TextBox_OS_Title_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_About_2.ForeColor = Color.Black Then
            Main.TextBox_About_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_Origin_2.ForeColor = Color.Black Then
            Main.TextBox_Origin_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_Version_2.ForeColor = Color.Black Then
            Main.TextBox_Version_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_Website_2.ForeColor = Color.Black Then
            Main.TextBox_Website_2.ForeColor = Color.Black
        End If
        If Not Main.TextBox_Desktop_2.ForeColor = Color.Black Then
            Main.TextBox_Desktop_2.ForeColor = Color.Black
        End If
    End Sub
    'Show/Hide Main Window
    Shared Sub Show_Hide_Main_Window()
        If Main.WindowState = FormWindowState.Normal Then
            Main.WindowState = FormWindowState.Minimized
            Main.Hide()
            Main.NotifyIcon_Main.BalloonTipText = "Minimized to tray."
            Main.NotifyIcon_Main.ShowBalloonTip(1500)
        Else
            Main.Show()
            Main.WindowState = FormWindowState.Normal
            Main.BringToFront()
            Main.Activate()
        End If
    End Sub
    'Save HTML 1
    Shared Sub Save_HTML_1()
        '
        If Not Directory.Exists(Application.StartupPath & "/Source/Website/operating_systems/") Then
            Directory.CreateDirectory(Application.StartupPath & "/Source/Website/operating_systems/")
        End If

        '
        If File.Exists(Application.StartupPath & "/Source/Website/operating_systems/" & Main.TextBox_OS_Title_1.Text & ".html") Then
            File.Delete(Application.StartupPath & "/Source/Website/operating_systems/" & Main.TextBox_OS_Title_1.Text & ".html")
        End If

        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "/Source/Website/operating_systems/" & "/" & Main.TextBox_OS_Title_1.Text & ".html", HTML.TextBox_HTML_Page.Text, True)
        Main.NotifyIcon_Main.BalloonTipText = "File: " & Main.TextBox_OS_Title_1.Text & ".html" & " Saved"
        Main.NotifyIcon_Main.ShowBalloonTip(1500)
        Main.NotifyIcon_Main.BalloonTipText = Nothing
        HTML.TextBox_HTML_Page.Text = Main.HTML_Page_Blank
    End Sub
    'Save HTML 2
    Shared Sub Save_HTML_2()
        '
        If Not Directory.Exists(Application.StartupPath & "/Source/Website/operating_systems/") Then
            Directory.CreateDirectory(Application.StartupPath & "/Source/Website/operating_systems/")
        End If

        '
        If File.Exists(Application.StartupPath & "/Source/Website/operating_systems/" & Main.TextBox_OS_Title_2.Text & ".html") Then
            File.Delete(Application.StartupPath & "/Source/Website/operating_systems/" & Main.TextBox_OS_Title_2.Text & ".html")
        End If

        My.Computer.FileSystem.WriteAllText(Application.StartupPath & "/Source/Website/operating_systems/" & "/" & Main.TextBox_OS_Title_2.Text & ".html", HTML.TextBox_HTML_Page.Text, True)
        Main.NotifyIcon_Main.BalloonTipText = "File: " & Main.TextBox_OS_Title_2.Text & ".html" & " Saved"
        Main.NotifyIcon_Main.ShowBalloonTip(1500)
        Main.NotifyIcon_Main.BalloonTipText = Nothing
        HTML.TextBox_HTML_Page.Text = Main.HTML_Page_Blank
    End Sub
    'Build HTML Page 1
    Shared Sub Build_HTML_Page_1()
        '
        HTML.TextBox_HTML_Page.Text = Main.HTML_Page_Blank
        '
        Main.HTML_Page = HTML.TextBox_HTML_Page.Text
        'About Game = 1239
        Main.HTML_Page = Main.HTML_Page.Insert(1110, Main.TextBox_About_1.Text)
        'Download-64bit button 1141
        Main.HTML_Page = Main.HTML_Page.Insert(1006, Main.TextBox_Download_64_Bit_1.Text)
        'Download-32bit button 1141
        Main.HTML_Page = Main.HTML_Page.Insert(911, Main.TextBox_Download_32_Bit_1.Text)
        'Origin
        Main.HTML_Page = Main.HTML_Page.Insert(853, Main.TextBox_Origin_1.Text)
        'Desktop
        Main.HTML_Page = Main.HTML_Page.Insert(792, Main.TextBox_Desktop_1.Text)
        'Version
        Main.HTML_Page = Main.HTML_Page.Insert(730, Main.TextBox_Version_1.Text)
        'IMG SRC
        Main.HTML_Page = Main.HTML_Page.Insert(625, Main.TextBox_OS_Title_1.Text.Replace(" ", "%20"))
        'A HREF
        Main.HTML_Page = Main.HTML_Page.Insert(529, Main.TextBox_Website_1.Text)
        'H1
        Main.HTML_Page = Main.HTML_Page.Insert(489, Main.TextBox_OS_Title_1.Text)
        'HTML Edited
        HTML.TextBox_HTML_Page.Text = Main.HTML_Page
    End Sub
    'Build HTML Page 2
    Shared Sub Build_HTML_Page_2()
        '
        HTML.TextBox_HTML_Page.Text = Main.HTML_Page_Blank
        '
        Main.HTML_Page = HTML.TextBox_HTML_Page.Text
        'About Game = 1239
        Main.HTML_Page = Main.HTML_Page.Insert(1110, Main.TextBox_About_2.Text)
        'Download-64bit button 1141
        Main.HTML_Page = Main.HTML_Page.Insert(1006, Main.TextBox_Download_64_Bit_2.Text)
        'Download-32bit button 1141
        Main.HTML_Page = Main.HTML_Page.Insert(911, Main.TextBox_Download_32_Bit_2.Text)
        'Origin
        Main.HTML_Page = Main.HTML_Page.Insert(853, Main.TextBox_Origin_2.Text)
        'Desktop
        Main.HTML_Page = Main.HTML_Page.Insert(792, Main.TextBox_Desktop_2.Text)
        'Version
        Main.HTML_Page = Main.HTML_Page.Insert(730, Main.TextBox_Version_2.Text)
        'IMG SRC
        Main.HTML_Page = Main.HTML_Page.Insert(625, Main.TextBox_OS_Title_2.Text.Replace(" ", "%20"))
        'A HREF
        Main.HTML_Page = Main.HTML_Page.Insert(529, Main.TextBox_Website_2.Text)
        'H1
        Main.HTML_Page = Main.HTML_Page.Insert(489, Main.TextBox_OS_Title_2.Text)
        'HTML Edited
        HTML.TextBox_HTML_Page.Text = Main.HTML_Page
    End Sub
    'Build Updated OS HTML
    Shared Sub Build_Updated_OS_HTML()
        '
        HTML.TextBox_Updated_OS_HTML.Text = Main.Updated_OS_HTML_Blank
        '
        Main.Updated_OS_HTML = HTML.TextBox_Updated_OS_HTML.Text
        'OS(Encoded) 100
        Main.Updated_OS_HTML = Main.Updated_OS_HTML.Insert(100, Main.TextBox_OS_Title_2.Text.Replace(" ", "%20"))
        'HTML Edited
        HTML.TextBox_Updated_OS_HTML.Text = Main.Updated_OS_HTML
    End Sub
    'Build OS HTML
    Shared Sub Build_OS_HTML()
        '
        HTML.TextBox_OS_HTML.Text = Main.OS_HTML_Blank
        '
        Main.OS_HTML = HTML.TextBox_OS_HTML.Text
        'OS Title 169
        Main.OS_HTML = Main.OS_HTML.Insert(169, Main.TextBox_OS_Title_2.Text)
        'Image Title 158
        Main.OS_HTML = Main.OS_HTML.Insert(158, Main.TextBox_OS_Title_2.Text)
        'OS(Encoded) 140
        Main.OS_HTML = Main.OS_HTML.Insert(140, Main.TextBox_OS_Title_2.Text.Replace(" ", "%20"))
        'Html Page 35
        Main.OS_HTML = Main.OS_HTML.Insert(35, Main.TextBox_OS_Title_2.Text)
        'HTML Edited
        HTML.TextBox_OS_HTML.Text = Main.OS_HTML
    End Sub
End Class